// 监听数据
import { createTransactionDetails } from '@/content-scripts/components/transaction/index.js'

export function observeTransactionDetailss() {
    let previousValues = {
        buyOrSell: null,
        timeSlot: null,
        startTime: null,
        endTime: null,
        transType: null
    };

    const observer = new MutationObserver(() => {
        // console.log(mutations,'mutations');

        const order = document.querySelectorAll('.order_types_container')[0];
        if (!order) return;

        const buyOrSell = order.querySelector('.mantine-wz61vw').value;
        const timeSlot = document.querySelector('.mantine-1t5amwr > .tr-border-c-brand-3');
        const startTime = document.querySelector('.mantine-1626jtb').value;
        const endTime = document.querySelector('.mantine-17whd58').value;
        const transType = document.querySelector('.mantine-15he3b8 > .gZDgCa');
        const pagePagination = JSON.parse(localStorage.getItem('pagePagination'))
        // 检查是否有值发生变化
        const hasChanged =
            buyOrSell !== previousValues.buyOrSell ||
            timeSlot !== previousValues.timeSlot ||
            startTime !== previousValues.startTime ||
            endTime !== previousValues.endTime ||
            transType !== previousValues.transType || pagePagination.isChange

        // 如果所有必需的元素都存在且值发生变化
        if (buyOrSell && timeSlot && startTime && endTime && transType && hasChanged) {
            // 存新值
            previousValues = {
                buyOrSell,
                timeSlot,
                startTime,
                endTime,
                transType,
                current: pagePagination.current,
                size: pagePagination.pageSize
            };
            console.log('99999999999333333333333333388899999');
            // console.log('值发生变化：', buyOrSell, timeSlot, startTime, endTime, transType);
            createTransactionDetails(previousValues);
        }
    });

    // 监听整个文档的变化
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true
    });
}

let inStockTableObserve = null

import { language } from '@/content-scripts/locales';
import { getElement } from '@/content-scripts/getElement.js';

export function observeTransactionDetails() {
    if (inStockTableObserve) inStockTableObserve.disconnect();

    let previousValues = {
        firstStageTabs: null
    };
    let num = 0
    inStockTableObserve = new MutationObserver(() => {
        const inStockTabs = getElement("transaction.table.tabs")[0]
        if (inStockTabs && inStockTabs.innerHTML) {
            num = 0
            // console.log(inStockTabs,'inStockItemTableObserver');
            // console.log(inStockTabs.innerHTML,'inStockItemTableObserver');

            // let tabBtnHtml = null
            // if (tabBtn.innerHTML.includes('span')) {
            //     tabBtnHtml = tabBtn.querySelector('span').innerHTML
            // } else {
            //     tabBtnHtml = tabBtn.innerHTML
            // }
            const hasChanged =
                inStockTabs.innerHTML != previousValues.firstStageTabs

            // console.log(pagePagination);
            // console.log(pagePagination.isChange);
            if (inStockTabs && hasChanged) {
                previousValues = {
                    firstStageTabs: inStockTabs.innerHTML,
                }
                // console.log(previousValues, 'previossssssssssss');
                // 生成表格
                tabsChange(inStockTabs.innerHTML)
            }
        } else {
            const smallArea = getElement("small.transaction.table")[0]
            if (smallArea && num == 0) {
                num += 1
                tabsChange(language.tradeHistory)
            }

        }

    });
    inStockTableObserve.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}

function tabsChange(value) {
    if (value.includes('当前委托')) {
        currentCommissonTable(value)
    } else if (value.includes('委托历史')) {
        commissionHistory(value)
    } else if (value.includes(language.tradeHistory)) {
        console.log(value,'transacRecordtransacRecordtransacRecord');

        transacRecord(value)
    } else if (value.includes('交易机器人')) {
        transacRobot(value)
    } else if (value.includes('借款管理')) {
        loanManage(value)
    } else if (value.includes('资产')) {
        if (inStockItemTableObserver) inStockItemTableObserver.disconnect();
        console.log('9999999999944444444444444888899999');
        createTransactionDetails({ firstStageTabs: value });
    }
}

let inStockItemTableObserver = null

function currentCommissonTable(value) {
    // console.log(value);
    if (inStockItemTableObserver) inStockItemTableObserver.disconnect();

    let previousValues = {
        commissionValue: null
    };

    inStockItemTableObserver = new MutationObserver(() => {
        const commissionParent = document.querySelector(
            ".spot_order_table > .bottom_box > .order_types_container > div > .mantine-15he3b8 > .gZDgCa"
        );
        const commissionType = commissionParent.querySelector('.mantine-w1aeuy > span')
        // console.log(commissionType, 'commissionType');
        const hasChanged =
            commissionType.innerHTML != previousValues.commissionValue
        if (commissionType && hasChanged) {
            previousValues = {
                firstStageTabs: value,
                commissionValue: commissionType.innerHTML
            }
            console.log('99999999999955555555555558888899999');
            // console.log(previousValues, 'previossssssssssss');
            // 生成表格
            createTransactionDetails(previousValues);
        }
    });
    inStockItemTableObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}

function commissionHistory(value) {
    // console.log(value);
    if (inStockItemTableObserver) inStockItemTableObserver.disconnect();

    let previousValues = {
        directionValue: null,
        // timeIntervalValue: null,
        startTimeValue: null,
        endTimeValue: null,
        commissionValue: null
    };

    inStockItemTableObserver = new MutationObserver(() => {
        const searchArea = document.querySelector('.spot_order_table > .bottom_box > div > .order_types_container')
        // console.log(searchArea,'searchArea');

        const directionInput = searchArea.querySelector('.mantine-125u5pf > div > .mantine-dcm9q0 > input')
        // console.log(directionInput, 'directionInput');

        // const timeIntervalType = searchArea.querySelector('.mantine-1t5amwr > .tr-text-c-text-5')
        // console.log(timeIntervalType, 'timeIntervalType');

        const timeGroup = searchArea.querySelectorAll('.trade-order-rangepicker-wrapper > div > div > .mantine-15kxfbj > .mantine-7lvkc8 > input')
        const startTime = timeGroup[0]
        const endTIme = timeGroup[1]
        // console.log(startTime, endTIme, 'startTime,endTIme');

        const commissionType = document.querySelector(
            ".spot_order_table > .bottom_box > div > .order_types_container > div > .mantine-15he3b8 > .gZDgCa > .mantine-w1aeuy > span"
        );
        // console.log(commissionType, 'commissionParent');
        const hasChanged =
            // timeIntervalType.innerHTML != previousValues.timeIntervalValue ||
            directionInput.value != previousValues.directionValue ||
            startTime.value != previousValues.startTimeValue ||
            endTIme.value != previousValues.endTimeValue ||
            commissionType.innerHTML != previousValues.commissionValue
        if (directionInput && startTime && endTIme && commissionType && hasChanged) {
            previousValues = {
                firstStageTabs: value,
                directionValue: directionInput.value,
                // timeIntervalValue: timeIntervalType.innerHTML,
                startTimeValue: startTime.value,
                endTimeValue: endTIme.value,
                commissionValue: commissionType.innerHTML
            }
            console.log('9999999999966666666666666888899999');
            // 生成表格
            createTransactionDetails(previousValues);
        }
    });
    inStockItemTableObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}


function transacRecord(value) {

    if (inStockItemTableObserver) inStockItemTableObserver.disconnect();

    let previousValues = {
        directionValue: null,
        // timeIntervalValue: null,
        startTime: null,
        endTime: null,
    };
    let num = 0
    console.log(num);

    inStockItemTableObserver = new MutationObserver(() => {

        const searchArea = getElement("transaction.table.searchArea")[0]

        if (searchArea) {
            num = 0
            const directionInput = getElement("transaction.table.directionInput", searchArea)[0]
            // searchArea.querySelector('.mantine-Input-wrapper > input')
            // console.log(directionInput, 'directionInput');

            // const timeIntervalType = searchArea.querySelector('.mantine-1t5amwr > .tr-text-c-text-5')
            // console.log(timeIntervalType, 'timeIntervalType');

            const timeGroup = getElement("transaction.table.timeGroup", searchArea)
            // searchArea.querySelectorAll('.gui_date_8eed51aa > .gui_date_bb5709f4 > .gui_date_2d338085 > input')
            const startTime = timeGroup[0]
            const endTIme = timeGroup[1]
            // console.log(startTime, endTIme, 'startTime,endTIme');
            const pagePagination = JSON.parse(localStorage.getItem('pagePagination'))

            const hasChanged =
                // timeIntervalType.innerHTML != previousValues.timeIntervalValue ||
                directionInput.value != previousValues.directionValue ||
                startTime.value != previousValues.startTime ||
                endTIme.value != previousValues.endTime || pagePagination.isChange
            // console.log(pagePagination.isChange, '999999999999999999');

            if (directionInput && startTime && endTIme && hasChanged) {
                previousValues = {
                    firstStageTabs: value,
                    directionValue: directionInput.value,
                    // timeIntervalValue: timeIntervalType.innerHTML,
                    startTime: startTime.value,
                    endTime: endTIme.value,
                    current: pagePagination.current,
                    size: pagePagination.pageSize
                }
                // 生成表格

                console.log('9999999999999887777777777777778888899999');
                createTransactionDetails(previousValues);
            }
        } else {
            const smallArea = getElement("small.transaction.table")[0]
            // document.querySelector('.bottom_box > .table-scroll-window > .tr-table__wrapper > .mantine-t7pwby')

            if (smallArea && num == 0) {
                const pagePagination = JSON.parse(localStorage.getItem('pagePagination'))
                const timeGroup = getElement("small.transaction.timeGroup", smallArea)
                // searchArea.querySelectorAll('.trade-order-rangepicker-wrapper > div > div > .mantine-15kxfbj > .mantine-7lvkc8 > input')
                const startTime = timeGroup[0]
                const endTIme = timeGroup[1]
                previousValues = {
                    firstStageTabs: value,
                    current: pagePagination.current,
                    size: pagePagination.pageSize,
                    startTime: startTime,
                    endTime: endTIme
                }
                num += 1
                console.log(previousValues, 'eeeeeerrrrrrrrrrtttttttttt');

                createTransactionDetails(previousValues);
            }

        }

    });
    inStockItemTableObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}

function loanManage(value) {
    if (inStockItemTableObserver) inStockItemTableObserver.disconnect();

    let previousValues = {
        loanValue: null,
        detailsSearchValue: null
    };
    inStockItemTableObserver = new MutationObserver(() => {
        const loanType = document.querySelector('.mantine-1xng1t9 > .mantine-ujq11l > button[data-active="true"] > div')


        let hasChanged =
            loanType.innerHTML != previousValues.loanValue
        let detailsSearchType = null

        if (previousValues.loanValue == '借款明细') {
            detailsSearchType = document.querySelector('.mantine-fwlbvi > div > .mantine-dcm9q0 > .mantine-wz61vw')
            hasChanged =
                loanType.innerHTML != previousValues.loanValue || detailsSearchType.value != previousValues.detailsSearchValue
        }
        if (loanType && hasChanged) {
            previousValues = {
                firstStageTabs: value,
                loanValue: loanType.innerHTML,
                detailsSearchValue: detailsSearchType ? detailsSearchType.value : null
            }
            console.log('99999999999998888811111111111118888899999');
            // console.log(previousValues, 'previossssssssssss');
            // 生成表格
            createTransactionDetails(previousValues);
        }
    });
    inStockItemTableObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}

function transacRobot(value) {
    if (inStockItemTableObserver) inStockItemTableObserver.disconnect();

    let previousValues = {
        transacValue: null
    };

    inStockItemTableObserver = new MutationObserver(() => {
        const searchArea = document.querySelector('.spot_order_table > .bottom_box > .bottom-table-container > .bZArcR > .mantine-1ryitj8 > .mantine-ydn5px > div > div ')
        const transacType = searchArea.querySelector('.mantine-14rgv10 > .mantine-1a44ght > button[data-active="true"] > .mantine-d4p53s > span')
        // console.log(transacType, 'transacType');
        const hasChanged =
            transacType.innerHTML != previousValues.transacValue
        if (transacType && hasChanged) {
            previousValues = {
                firstStageTabs: value,
                transacValue: transacType.innerHTML
            }
            // console.log(previousValues, 'previossssssssssss');
            console.log('999999999999222222222222228888899999');
            // 生成表格
            createTransactionDetails(previousValues);
        }
    });
    inStockItemTableObserver.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: true,
        characterData: true,
    });
}